/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Processor.Components;

import Config.ConfigSetting;
import Control.MainControl.MainControl;
import Data.ActivityLog.ActivityCrl;
import Data.Frontier.URLCrl;
import Data.ProcessData.ProcessData;
import DataAccessor.DatabaseAccessor;
import Downloader.ImageDownloader;
import ExtractorInterface.ExtractorInterface;
import java.util.Hashtable;

/**
 *
 * @author CUHUUHOANG
 */
public class ExtractContent {

    ProcessData pData;
    ExtractorInterface extractor;

    public ExtractContent(ProcessData pData, ExtractorInterface extractor) {
        this.pData = pData;
        this.extractor = extractor;
    }

    public void extractContent() {


        try {
            //check co phai trang co noi dung ko
            if(!extractor.hasContent()) return;
            
            //lay thong tin
            Hashtable<String, String> out= extractor.getData();
            //download anh
            String[] imageUrl= extractor.getImageLink();
            String[] outPath= new String[imageUrl.length];
            if(ConfigSetting.downloadImage&&imageUrl.length>0){
                ImageDownloader imgDlder= new ImageDownloader();
                for (int i = 0; i < imageUrl.length; i++) {
                    outPath[i]=imgDlder.DownloadImage(imageUrl[i],ConfigSetting.imageDir);
                }                
            }
            //them anh vao hash
            if(outPath.length>0){
                String outputData= outPath[0];
                for (int i = 1; i < outPath.length; i++) {
                    if(outPath[i]!="")outputData+="|"+outPath[i];
                }
                if(outputData!=null)
                out.put(MainControl.extractorData.getImageAtrributeName(), outputData);
            }
            //chuyen cho database
            DatabaseAccessor da= new DatabaseAccessor();
            da.AddRow(out);
            //?ghi len man hinh
            
            //viet log
            ActivityCrl.atiData.addAtivity("Processing with url: " + pData.getUrl());
            URLCrl.urlDT.increaseURLProcessed();
            
        } catch (Exception e) {
            ActivityCrl.atiData.addAtivity(e.getMessage());
        }

    }
}
